<template>
  <div class="dashboard-editor-container">
    <div class=" clearfix">
      <div class="pan-item" >
        <div class="pan-info"></div>
        <img class="pan-thumb" :src="avatar">
      </div>
      <div class="info-container">
        <!--<span class="display_name">{{nick}}</span>-->
        <div style="font-size: 38px;line-height: 38px;color: #212121;padding-top: 15px;">
          {{nick}}
        </div>
        <div>
          <span style="font-size:20px;padding-top:20px;display:inline-block;">角色：</span>
          <el-tag style="margin-right: 5px;" type="danger" v-if="roles.length==0" >游客（未配置任何角色）</el-tag>
          <el-tag style="margin-right: 5px;" type="success" v-else v-for="r in roles" :key="r.val">{{r.name}}</el-tag>
        </div>
        <div>
          <span style="font-size:20px;padding-top:20px;display:inline-block;">联系电话：</span>
          <el-tag style="margin-right: 5px;" type="success" >{{phone}}</el-tag>
        </div>
        <div>
          <span style="font-size:20px;padding-top:20px;display:inline-block;">身份证号：</span>
          <el-tag style="margin-right: 5px;" type="success">{{identityCard}}</el-tag>
        </div>
        <div style="margin-top: 20px">
          <el-button @click="dialogPhotoVisible = true" type="primary" size="mini" round>修改头像</el-button>
          <el-button @click="dialogPasswordVisible = true" type="warning" size="mini" round>重置密码</el-button>
        </div>
      </div>
    </div>
    <div>
      <el-dialog title="重置密码" :visible.sync="dialogPasswordVisible" width="40%">
        <el-form :model="passwordForm" ref="passwordForm" label-width="100px">
          <el-row>
            <el-col :span="20">
              <el-form-item
                label="原密码："
                prop="lastPassword"
                :rules="[{required: true, trigger: 'blur', validator: validatePassword}]">
                <el-input v-model="passwordForm.lastPassword" show-password></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="20">
              <el-form-item
                label="新密码："
                prop="firstPassword"
                :rules="[{required: true, trigger: 'blur', validator: validatePassword}]">
                <el-input v-model="passwordForm.firstPassword" @blur="firstPasswordChange" show-password></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="20">
              <el-form-item
                label="确认密码："
                prop="secondPassword"
                :rules="[{required: true, trigger: 'blur', validator: validatePassword}]">
                <el-input v-model="passwordForm.secondPassword" show-password></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="cacnlPassword">取 消</el-button>
          <el-button type="primary" @click="subimitPassword">确 定</el-button>
        </div>
      </el-dialog>
    </div>
    <div>
      <el-dialog title="修改头像" :visible.sync="dialogPhotoVisible" width="40%">
        <el-form :model="photoForm" ref="photoForm" label-width="100px">
          <el-row>
            <el-col :span="20">
              <el-form-item
                label="选择头像："
                prop="photo">
                <el-upload
                  class="avatar-uploader"
                  action="http://localhost:8888/sysUser/data/uploadFile"
                  :headers="uploadHeaders"
                  :show-file-list="false"
                  :on-success="successImgList"
                  :before-upload="beforeAvatarUpload">
                  <img v-if="imgUid.url" :src="imgUid.url" class="avatar">
                  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="cacnlPhoto">取 消</el-button>
          <el-button type="primary" @click="subimitPhoto">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>


<script>
  import { mapGetters } from 'vuex'
  import PanThumb from '@/components/PanThumb'
  import userApi from '@/api/backstage/user'
  import { getToken } from '@/utils/auth'
  export default {
    name: 'dashboard',
    components: { PanThumb },
    data() {
      return {
        uploadHeaders: {
          'user-token':getToken()
        },
        dialogPasswordVisible:false,
        passwordForm:{
          lastPassword:'',
          firstPassword:'',
          secondPassword:'',
        },
        dialogPhotoVisible: false,
        photoForm:{
          photo:'',
        },
        imgUid:{
          url:'',
        },
      }
    },
    methods: {
      validatePassword(rule, value, callback){
        let passwordRule = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{6,16}$/
        // if (value.length < 6||value.length >16) {
        if (!passwordRule.test(value)) {
          callback(new Error('密码由6-16个包含大小写和数字的字符组成'));
        } else {
          callback()
        }
      },
      firstPasswordChange(value){
        // console.log(value)
      },
      cacnlPassword(){
        this.dialogPasswordVisible = false;
        this.$nextTick(() => this.$refs['passwordForm'].clearValidate());
        this.passwordForm = {
          lastPassword:'',
          firstPassword:'',
          secondPassword:'',
        }
      },
      subimitPassword(){
        this.$refs['passwordForm'].validate(valid => {
          if (valid){
            if (this.passwordForm.firstPassword == this.passwordForm.lastPassword) {
              this.$notify.warning({title:'警告', message:'原密码不能与修改密码相同！'});
              return false;
            }
            if (this.passwordForm.firstPassword != this.passwordForm.secondPassword) {
              this.$notify.warning({title:'警告', message:'修改密码不一致！'});
              return false;
            }
            let params = {
              lastPassword: this.passwordForm.lastPassword,
              firstPassword: this.passwordForm.firstPassword,
            }
            userApi.restPassword(params).then(res => {
              this.dialogPasswordVisible = false;
              this.$notify.success({title:'成功', message:'密码重置成功！'});
              this.passwordForm = {
                lastPassword:'',
                firstPassword:'',
                secondPassword:'',
              }
            }).catch(() => {
              this.$notify.error({title:'错误', message:'系统繁忙，请稍后再试！'});
            })
          } else {
            this.$notify.warning({title:'提示', message:'输入的密码信息不合规！'});
          }
        });
      },
      /** 图片 **/
      successImgList(response, file, fileList){
         this.imgUid = {
          uid: file.uid,
          name: file.name,
          url: response.data[file.name]
        }
      },
      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg';
        const isLt1M = file.size / 1024 / 1024 < 2;
        this.beforeUploadImgRule(isJPG,"JPG",isLt1M,"2MB")
        return isJPG && isLt1M;
      },
      cacnlPhoto(){
        this.dialogPhotoVisible = false;
        this.imgUid = {
          url:'',
        }
      },
      subimitPhoto(){
        if (this.imgUid.url===''){
          this.$notify.warning({title:'提示', message:'上传图片！'});
          return false;
        }
        let params = {
          photo: this.imgUid.url,
        }
        userApi.restPassword(params).then(res => {
          this.dialogPhotoVisible = false;
          this.$notify.success({title:'成功', message:'头像重置成功！'});
          this.imgUid = {
            url:'',
          }
          this.$store.dispatch('GetUserInfo').then(res => {});
        }).catch(() => {
          this.$notify.error({title:'错误', message:'系统繁忙，请稍后再试！'});
        })

      },
      /** 规则校验 **/
      beforeUploadImgRule(isFormat,formatValue, isLt, ltValue){
        // 图片格式、大小规则检验
        if (!isFormat) {
          this.$notify.warning({title:'提示', message:'上传图片只能是 '+formatValue+' 格式!'});
        }
        if (!isLt) {
          this.$notify.warning({title:'提示', message:'上传图片大小不能超过 '+ltValue+'!'});
        }
      },
      valueLengthRule(value, name, maxlength){
        if (value.length > maxlength){
          return new Error(name + '不能超过'+ maxlength +'个字')
        }
      },
    },
    computed: {
      ...mapGetters([
        'name',
        'nick',
        'avatar',
        'phone',
        'identityCard',
        'roles',
        'perms'
      ])
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .pan-info {
    position: absolute;
    width: inherit;
    height: inherit;
    border-radius: 50%;
    overflow: hidden;
    box-shadow: inset 0 0 0 5px rgba(0, 0, 0, 0.05);
  }
  .pan-item {
    float: left;
    z-index: 1;
    width: 150px;
    height: 150px;
    border-radius: 50%;
    display: inline-block;
    position: relative;
    cursor: default;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
  }
  .pan-thumb {
    width: 100%;
    height: 100%;
    background-size: 100%;
    border-radius: 50%;
    overflow: hidden;
    position: absolute;
    transform-origin: 95% 40%;
    transition: all 0.3s ease-in-out;
  }

  .emptyGif {
    display: block;
    width: 45%;
    margin: 0 auto;
  }

  .dashboard-editor-container {
    background-color: #F2F6FC;
    min-height: 100vh;
    padding: 50px 60px 0px;
  .pan-info-roles {
    font-size: 12px;
    font-weight: 700;
    color: #333;
    display: block;
  }
  .info-container {
    position: relative;
    margin-left: 190px;
    height: 80px;
  /*line-height: 80px;*/
  .display_name {
    font-size: 38px;
    line-height: 38px;
    color: #212121;
    position: absolute;
    top: 25px;
  }
  }
  }
</style>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>

